package com.liuzu.esys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.bo.RefundAfterSaleBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.mapper.RefundAfterSaleMapper;
import com.liuzu.esys.pojo.RefundAfterSale;
import com.liuzu.esys.service.RefundAfterSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("/refundAfterSaleService")
public class RefundAfterSaleServiceImpl implements RefundAfterSaleService {
    @Autowired
    private RefundAfterSaleMapper refundAfterSaleMapper;
    //分页展示数据和模糊查询的接口
    @Override
    public LayuiPageVo<RefundAfterSale> refundAfterSalePageData(Map<String, Object> params) {

        // mybatis 的 分页插件
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");
        PageHelper.startPage(page,limit);

        List<RefundAfterSale> refundAfterSales = refundAfterSaleMapper.selectByParams(params);
        // mybatis 的 分页数据结构
        PageInfo<RefundAfterSale> refundAfterSalePageInfo = new PageInfo<>(refundAfterSales);
        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中

        LayuiPageVo<RefundAfterSale> refundAfterSaleLayuiPageVo = new LayuiPageVo<>();
        refundAfterSaleLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        refundAfterSaleLayuiPageVo.setMsg("分页列表数据"); // 提示信息
        refundAfterSaleLayuiPageVo.setCount(refundAfterSalePageInfo.getTotal()); // 总记录数
        refundAfterSaleLayuiPageVo.setData(refundAfterSalePageInfo.getList());  // 分页的列表数据
        return refundAfterSaleLayuiPageVo;
    }

    @Override
    public int refundAfterSaleAdd(RefundAfterSale refundAfterSale) { return refundAfterSaleMapper.refundAfterSaleUpdate(refundAfterSale); }

    @Override
    public List<RefundAfterSaleBo> refundAfterSaleExcelData(RefundAfterSale refundAfterSale) {
        List<RefundAfterSale> refundAfterSales = refundAfterSaleMapper.selectBykey(refundAfterSale);
        List<RefundAfterSaleBo> refundAfterSaleBos = refundAfterSales.stream()
                .map((dr) -> {
                    RefundAfterSaleBo refundAfterSaleBo = new RefundAfterSaleBo();
                    refundAfterSaleBo.setOrder_number(dr.getOrder_number());
                    refundAfterSaleBo.setAfter_sale_status(dr.getAfter_sale_status());
                    refundAfterSaleBo.setAfter_sale_type(dr.getAfter_sale_type());
                    refundAfterSaleBo.setRefund_money(dr.getRefund_money());
                    refundAfterSaleBo.setNotes(dr.getNotes());
                    refundAfterSaleBo.setComment_time(dr.getCreate_time());
                    return refundAfterSaleBo;
                }).collect(Collectors.toList());
        return refundAfterSaleBos;
    }

    @Override
    public int refundAfterSaleDelete(int order_number) {
        return refundAfterSaleMapper.refundAfterSaleDelete(order_number);
    }

    @Override
    public int refundAfterSaleUpdate(RefundAfterSale refundAfterSale) { return refundAfterSaleMapper.refundAfterSaleUpdate(refundAfterSale); }

}
